package com.ax.action.system;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.ax.pojo.TabSysUsers;
import com.ax.service.TabSysUsersService;
import com.ax.utils.BaseAction;
import com.ax.utils.Keys;
import com.ax.utils.PageInfo;

@Controller("tabSysUsersAction")
public class TabSysUsersAction extends BaseAction {
   
	
	private TabSysUsersService tabSysUsersService;
	
	private PageInfo page;
	public PageInfo getPage() {
		return page;
	}

	public void setPage(PageInfo page) {
		this.page = page;
	}

	@Resource(name="tabSysUsersService")
	public void setTabSysUsersService(TabSysUsersService tabSysUsersService) {
		this.tabSysUsersService = tabSysUsersService;
	}

	public String login(){
	   String uname=request.getParameter("uname");
	   String upassword=request.getParameter("upassword");
	   String verifycode=request.getParameter("verifycode");
	   
	   if(uname==null||"".equals(uname)){
		   session.setAttribute("error", "请输入管理员");
	   }else if(upassword==null||"".equals(upassword)){
		   session.setAttribute("error", "请输入密码");
	   }else if(getSession().getAttribute(Keys.verifyCode)==null||!getSession().getAttribute(Keys.verifyCode).toString().equals(verifycode)){
		   session.setAttribute("error", "验证码错误");
	   }else{
		     TabSysUsers sysUsers=tabSysUsersService.getTabSysUsers(uname);
		     if(sysUsers==null){
		    	 session.setAttribute("error", "账号密码错误");
		     }else{
		    	 if(sysUsers.getUstatus().equals(0)){
		    		 session.setAttribute("error", "当前用户被禁用");
		    	 }else{
		    		 if(!sysUsers.getUpassword().equals(upassword)){
			    		 session.setAttribute("error", "账号密码错误");
			    	 }else{
			    		 session.setAttribute(Keys.USER_ID, sysUsers);
			    		 return "loginsuccess";
			    	 }
		    	 }
		     }
	   }
	   
		return "loginError";
	}
	
	public String exitLogin(){
		session.removeAttribute(Keys.USER_ID);
		return "loginExit";
	}
	
	
	public String findSysUsers(){
		if(page==null){
			page=new PageInfo();
		}
		String uname=request.getParameter("uname");
		String code="";
		if(uname!=null&&!"".equals(uname)){
			code=" and po.uname='"+uname+"'  ";
		}
		page=tabSysUsersService.findPageInfo(page.getCurPage(), code);
		return "admin_admin";
	}
	
	
	public String findIsExits(){
		String uname=request.getParameter("uname");
		TabSysUsers sysUsers=tabSysUsersService.getTabSysUsers(uname);
		boolean flag=false;
		if (sysUsers==null) {
			  flag=true;
		}
		out.println(flag);
		return null;
	}
	
	public String update(){
		  String uname=request.getParameter("uname");
		  String uid=request.getParameter("uid");
		  String upassword=request.getParameter("upassword");
		  int ustatus=Integer.parseInt(request.getParameter("ustatus"));
		  TabSysUsers sysUsers=tabSysUsersService.getTabSysUsers(uname);
		  sysUsers.setUpassword(upassword);
		  sysUsers.setUstatus(ustatus);
		  tabSysUsersService.updateSysUsers(sysUsers);
		return "admin_list";
	}
	
	
	public String save(){
		  String uname=request.getParameter("uname");
		  String upassword=request.getParameter("upassword");
		  int ustatus=Integer.parseInt(request.getParameter("ustatus"));
		  TabSysUsers sysUsers=new TabSysUsers();
		  sysUsers.setUname(uname);
		  sysUsers.setUpassword(upassword);
		  sysUsers.setUstatus(ustatus);
		  tabSysUsersService.saveSysUsers(sysUsers);
		return "admin_list";
	}
}
